/**
 * 富文本编辑器
 * Created by jiangj01 on 15/12/03.
 */
define(function (require) {
    require('angular-mocks');
    require('static_public/js/angular_directive/ueditor');

    window.UEDITOR_CONFIG.serverUrl = '';

    /**
     * @param {jQuery} element
     * @param {string} html
     * @returns {string}
     */
    function editorHtml(element, html) {
        if (typeof html !== 'undefined') {
            element.find('iframe')[0].contentWindow.window.document.body.innerHTML = html;
        } else {
            return element.find('iframe')[0].contentWindow.window.document.body.innerHTML;
        }
    }

    describe('ueditor', function () {
        var scope, $compile, element, editor;
        beforeEach(module('angular.directive.ueditor'));
        beforeEach(function (done) {
            inject(function ($rootScope, _$compile_, _$document_) {
                scope = $rootScope.$new(true);
                $compile = _$compile_;
                element = angular.element('<div ueditor ng-model="value" uid="uid"></div>');
                _$document_.find('body').append(element);

                $compile(element)(scope);
                scope.$digest();

                editor = UE.instants['ueditorInstant' + scope.uid];

                editor.ready(function () {
                    editor.addListener('contentChange', function () {
                        done();
                    });
                });
            });
        });

        afterEach(function () {
            element.remove();
            editor = null;

            scope.$destroy();
            scope = null;
        });

        it('初始化', function (done) {
            expect(editor).not.toBeNull();

            done();
        });

        it('赋值', function (done) {
            var htmlText = 'html文字1231132';

            editor.addListener('contentChange', function () {
                expect(editorHtml(element)).toContain(htmlText);
                done();
            });

            scope.value = htmlText;
            scope.$digest();
        });

        it('输入html', function (done) {
            var htmlText = '<div>html文字</div>';

            editor.addListener('contentChange', function () {
                setTimeout(function () {
                    expect(scope.value).toEqual(htmlText);

                    done();
                }, 0);
            });

            editorHtml(element, htmlText);
            editor.fireEvent('contentChange');
        });
    });
});